package com.cardbaobao.cardbabyclient.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.model.QuickDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.result.QuickDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/16 15:14.
 */

public class AddCreditCardSuccessActivity extends BaseActivity{
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_credit_card_success;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mCurrentPageId= Self.Page.app_add_credit_card_success;
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        GoogleAnalyticsUtil.setScreenName("信用卡添加成功");
    }

    @OnClick(R.id.bt_debit_now)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_debit_now:
                showProgressDialog(false);
                Api.getQuickDebitFeeRate().subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        dismissProgressDialog();
                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                getString(R.string.string_request_data_fail));
                    }

                    @Override
                    public void onNext(String s) {
                        dismissProgressDialog();
                        QuickDebitFeeRateResult result= JsonUtil.json2Object(s,QuickDebitFeeRateResult.class);
                        if(result.getCode()== Constant.SUC_CODE){
                            QuickDebitFeeRateData data=result.getData();
                            if(data!=null){
                                ArrayList<QuickDebitFeeRateData.FeeRate> rates=data.getRate_list();
                                if(rates!=null&&rates.size()>0){
                                    boolean canQuickDebit=false;
                                    for(QuickDebitFeeRateData.FeeRate r:rates){
                                        if(Constant.KQB010101.equals(r.getChannel_type())){
                                            canQuickDebit=true;
                                            break;
                                        }
                                    }
                                    if(canQuickDebit){
                                        Intent intent = new Intent();
                                        intent.putParcelableArrayListExtra(FeeRateActivity.QUICK_DEBIT_FEE_RATE_LIST_ARRAY,
                                                rates);
                                        intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                                FeeRateActivity.QUICK_DEBIT_FEE_TYPE);
                                        intent.setClass(mContext,QuickDebitActivity.class);
                                        intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                                        startActivity(intent);
                                        finish();
                                    }else{
                                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                                getString(R.string.string_data_exception));
                                    }
                                }
                            }
                        }else{
                            ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                    getString(R.string.string_request_data_fail));
                        }
                    }
                });
                break;
        }
    }
}
